package com.zhen.interceptor.thread;

import lombok.extern.slf4j.Slf4j;

import java.util.Random;
import java.util.concurrent.Callable;

@Slf4j
public class MySumTask implements Callable<Integer> {
    private int startNum;
    private int endNum;

    public MySumTask(int startNum, int endNum) {
        this.startNum = startNum;
        this.endNum = endNum;
    }


    /**
     * 具体的业务逻辑实现，
     *
     * @return
     * @throws Exception
     */
    @Override
    public Integer call() throws Exception {
        int sum = 0;
        for (int i = startNum; i <= endNum; i++) {
            sum += i;
        }
        String threadName = Thread.currentThread().getName();
        int sleep = new Random().nextInt(1000);
        Thread.sleep(sleep);
        log.info("线程{}等待时间是{}，执行的是从{}到{}，执行的结果是{}", threadName, sleep, startNum, endNum, sum);
        return sum;
    }
}
